home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / TTYDRIV.C < prev    next >
C/C++ Source or Header  |  1988-11-30  |  2KB  |  125 lines

  1. /* TTY input driver */
  2. #include <stdio.h>
  3.  
  4. #include "global.h"
  5.  
  6. int ttymode;
  7. #define TTY_COOKED    0
  8. #define    TTY_RAW    1
  9.  
  10. int ttyecho;
  11. #define    TTY_NOECHO    0
  12. #define    TTY_ECHO    1
  13.  
  14. #define    LINESIZE    256
  15.  
  16. #define    CTLU    21
  17. #define CTLR    18
  18.  
  19. raw()
  20. {
  21.     ttymode = TTY_RAW;
  22. }
  23.  
  24. cooked()
  25. {
  26.     ttymode = TTY_COOKED;
  27. }
  28.  
  29. /* Accept characters from the incoming tty buffer and process them
  30.  * (if in cooked mode) or just pass them directly (if in raw mode).
  31.  * Returns the number of characters available for use; if non-zero,
  32.  * also stashes a pointer to the character(s) in the "buf" argument.
  33.  */
  34.  /*Control-R added by df for retype of lines - useful in Telnet */
  35.  
  36. void
  37. echo()
  38. {
  39.     ttyecho = TTY_ECHO;
  40. }
  41.  
  42. void
  43. noecho()
  44. {
  45.     ttyecho = TTY_NOECHO;
  46. }
  47.  
  48. int
  49. ttydriv(c,buf)
  50. char c;
  51. char **buf;
  52. {
  53.     static char linebuf[LINESIZE];
  54.     static char *cp = linebuf;
  55.     char *rp ;
  56.     int cnt;
  57.  
  58.     if(buf == (char **)NULL)
  59.         return 0;    /* paranoia check */
  60.  
  61.     cnt = 0;
  62.     switch(ttymode){
  63.     case TTY_RAW:
  64.         *cp++ = c;
  65.         cnt = cp - linebuf;
  66.         cp = linebuf;
  67.         break;
  68.     case TTY_COOKED:
  69.         /* Perform cooked-mode line editing */
  70.         switch(c & 0x7f){
  71.         case '\r':    /* CR and LF are equivalent */
  72.         case '\n':
  73.             *cp++ = '\r';
  74.             *cp++ = '\n';
  75.             printf("\n");
  76.             cnt = cp - linebuf;
  77.             cp = linebuf;
  78.             break;
  79.         case '\b':        /* Backspace */
  80.             if(cp != linebuf){
  81.                 cp--;
  82.                 if (ttyecho)
  83.                     printf("\b \b");
  84.             }
  85.             break;
  86.         case CTLR:    /* print line buffer */
  87.             if(ttyecho)
  88.                 printf("^R");
  89.             printf("\n");
  90.             if(ttyecho) {
  91.                 rp = linebuf ;
  92.                 while (rp < cp)
  93.                     putchar(*rp++) ;
  94.             }
  95.             break ;
  96.         case CTLU:    /* Line kill */
  97.             if(ttyecho) {
  98.                 while(cp != linebuf){
  99.                     cp--;
  100.                     printf("\b \b");
  101.                 }
  102.             } else
  103.                 cp = linebuf;
  104.             break;
  105.         default:    /* Ordinary character */
  106.             *cp++ = c;
  107. #ifndef    AMIGA
  108.             if (ttyecho)
  109.                 putchar(c);
  110. #endif
  111.             if(cp >= &linebuf[LINESIZE]){
  112.                 cnt = cp - linebuf;
  113.                 cp = linebuf;
  114.             }
  115.             break;
  116.         }
  117.     }
  118.     if(cnt != 0)
  119.         *buf = linebuf;
  120.     else
  121.         *buf = NULLCHAR;
  122.     fflush(stdout);
  123.     return cnt;
  124. }
  125.